package suzf.top.kotlinjava.common.exception

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.HttpMediaTypeNotSupportedException
import org.springframework.web.HttpRequestMethodNotSupportedException
import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.servlet.NoHandlerFoundException
import suzf.top.kotlinjava.common.config.CommonConstant
import suzf.top.kotlinjava.common.config.RespResult

/**
 *  定义全局异常
 *========================
 * Created with IntelliJ IDEA
 * Author:Mr.Su
 * Date:2019/9/23
 * Time:13:33
 * ========================
 */
@ControllerAdvice
@ResponseBody
class GlobalExceptionAdvice {

    private val logger: Logger = LoggerFactory.getLogger(GlobalExceptionAdvice::class.java)

    /**
     * 业务异常
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(ServiceException::class)
    fun handlerServiceException(e: ServiceException): RespResult {
        logger.error("业务异常", e)
        return RespResult(CommonConstant.INTERNAL_SERVER_ERROR, e.message, false)
    }

    /**
     * 400 -Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestParameterException::class)
    fun handleMissingServletRequestParameterException(e: MissingServletRequestParameterException): RespResult {
        logger.error("缺少请求参数", e)
        return RespResult(CommonConstant.BAD_REQUEST, "缺少请求参数: ${e.parameterName},类型:${e.parameterType}", false)
    }

    /**
     * 400 -Bad Request
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException::class)
    fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException): RespResult {
        logger.error("参数解析失败", e)
        return RespResult(CommonConstant.BAD_REQUEST, "参数解析失败: ${e.message}", false)
    }

    /**
     * 404 - Not Found
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException::class)
    fun noHandlerFoundException(e: NoHandlerFoundException): RespResult {
        logger.error("Not Fount", e)
        return RespResult(CommonConstant.NOT_FOUND, "Not Found: ${e.requestURL}", false)
    }

    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException::class)
    fun handleHttpRequestMethodNotSupportedException(e: HttpRequestMethodNotSupportedException): RespResult {
        logger.error("不支持当前请求方法", e)
        return RespResult(CommonConstant.METHOD_NOT_ALLOWED, "Method Not Allowed: ${e.message}", false)
    }

    /**
     * 415 - Unsupported Media Type
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException::class)
    fun handleHttpMediaTypeNotSupportedException(e: HttpMediaTypeNotSupportedException): RespResult {
        logger.error("不支持当前媒体类型", e)
        return RespResult(CommonConstant.UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type: ${e.message}", false)
    }
}
